package com.xxcxcxcx.jobsystem.common.Job.Lumberman;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.util.LazyOptional;

import static net.minecraft.world.effect.MobEffects.DIG_SPEED;

public class Lumberman extends JobBase {
    public static String Job= JobsClass.Lumberman;
    public static float GetMineSpeed(Player player , BlockState blockstate , float oldspeed){
        float newspeed=oldspeed;
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return newspeed;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        int level =playerSaveData.getLevel(Job);
        if(GetWood(blockstate)){
            newspeed *=GetUP2(player);
        }
        else if(level>=7 && GetLeaves(blockstate)){
            newspeed *=GetUP2(player);
        }
        return newspeed;
    }
    private static double GetUP2(Player player){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 1;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        int level =playerSaveData.getLevel(Job);
        double num=1.0d;
        if(level<4){
            num *= (level+1)*0.20;
        }
        if(level>=4){
            num *= (1+(level - 4) * 0.2);
        }
        if(level==10&&player.getRandom().nextInt(10)==0){
            num *= 10;
        }
        return num;
    }
    public static int getLootCount(Player player, BlockState blockState){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 1;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        int num = 1;
        if(GetLeaves(blockState)){
            int levelJob =playerSaveData.getLevel(Job);
            if(levelJob>=7){
                num += levelJob/2;
            }
        }
        return num;
    }
    public static void DropMore(BlockState blockState,Player player) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        int joblevel = playerSaveData.getLevel(Job);
        if(joblevel>=7&&player.getRandom().nextInt(20-joblevel)==1)player.addEffect(new MobEffectInstance(DIG_SPEED,100,1));
        if(joblevel==10&&player.getRandom().nextInt(20-joblevel)==1)player.addEffect(new MobEffectInstance(DIG_SPEED,100,2));
        if(GetWood(blockState)){
            UpExperience(player,5,Job);
        }
        if(GetLeaves(blockState)){
            UpExperience(player,0.3,Job);
        }
    }
    public static boolean GetRightBlock(BlockState block){
        return GetWood(block)||GetLeaves(block);
    }
    public static boolean GetWood(BlockState blockState){
        return blockState.getMaterial()== Material.WOOD;
    }
    public static boolean GetLeaves(BlockState blockState){
        return blockState.getBlock() instanceof LeavesBlock;
    }
    private static int GetUP(Player player){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 0;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        int farmlevel =playerSaveData.getLevel(Job);
        int num=0;
        if(farmlevel<4){
            num -= (player.getRandom().nextDouble()<(1 - 0.25 * farmlevel)?1:0);
        }
        if(farmlevel>=4){
            num += (player.getRandom().nextDouble()<(0.25 * (farmlevel-3))?1:0);
        }
        if(farmlevel>=7){
            num += (player.getRandom().nextDouble()<(0.10 * farmlevel)?1:0);
        }
        if(farmlevel>=10){
            num += (player.getRandom().nextDouble()<(0.05 * farmlevel)?1:0);
        }
        return num;
    }
    public static Block[] blocks={Blocks.OAK_LOG,Blocks.SPRUCE_LOG,Blocks.BIRCH_LOG,Blocks.JUNGLE_LOG,Blocks.ACACIA_LOG,Blocks.DARK_OAK_LOG};
    public static Item[] tools={Items.WOODEN_HOE,Items.STONE_HOE,Items.IRON_HOE,Items.GOLDEN_HOE,Items.DIAMOND_HOE,Items.NETHERITE_HOE};
    public static Item[] sapling={Items.OAK_SAPLING,Items.SPRUCE_SAPLING,Items.BIRCH_SAPLING,Items.JUNGLE_SAPLING,Items.ACACIA_SAPLING,Items.DARK_OAK_SAPLING};
    public static void Get(Player player,BlockPos pos){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.resolve().get();
        if(playerSaveData.getExperienceCD(Job)!=0)return;
        if(playerSaveData.getLevel(Job)!=10)return;
        BlockState blockState = player.level.getBlockState(pos);
        Item tool = player.getMainHandItem().getItem();
        boolean flag1=false;
        int count=0;
        boolean flag2=false;
        for (int i=0;i<blocks.length;i++){
            if(blocks[i]==blockState.getBlock()){
                count=i;
                flag1=true;
                break;
            }
        }
        for (Item item:tools){
            if(item==tool){
                flag2=true;
                break;
            }
        }
        if(flag1&&flag2){
            Block.popResource(player.level, pos, new ItemStack(sapling[count],1));
            playerSaveData.setExperienceCD(Job,1200);
        }
    }
}
